package com.lwh.springboot.controller;
import com.lwh.springboot.pojo.entity.CarType;
import com.lwh.springboot.pojo.vo.CarVoThree;
import com.lwh.springboot.pojo.vo.CarVoTwo;
import com.lwh.springboot.service.ICarService;
import com.lwh.springboot.pojo.entity.Car;
import com.lwh.springboot.service.ICarTypeService;
import com.lwh.springboot.util.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private ICarService carService;
    @Autowired
    private ICarTypeService carTypeService;

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model){
        Car car=carService.selectById(id);
        CarVoTwo carVoTwo = new CarVoTwo();
        carVoTwo.setCarid(Integer.parseInt(car.getCarid()));
        BeanUtils.copyProperties(car,carVoTwo);
        model.addAttribute("car",carVoTwo);
        List<CarType> list=carTypeService.selectAll();
        model.addAttribute("list",list);
        return "/car/update";
    }
    @RequestMapping("/update")
    public String update(Car car){
        carService.update(car);
        return "redirect:/car/selectByPage";
    }
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<CarType> list=carTypeService.selectAll();
        model.addAttribute("list",list);
        return "/car/add";
    }
    @RequestMapping("/add")
    public String add(Car car){
        carService.add(car);
        return "redirect:/car/selectByPage";
    }
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        carService.deleteById(id);
        return "redirect:/car/selectByPage";
    }
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        carService.deleteAll(ids);
        return "redirect:/car/selectByPage";
    }
    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "5") Integer pageSize, Model model,String carp,
                               Integer carid,String rent,Integer status) {
        System.out.println("CarController.selectByPage");
        if (null==rent || "".equals(rent)){
            rent = "0";
        }
        if (carid!=null&&!"".equals(carid)){
            if (carp.equals("null")){
                carp = "";
            }
        }
        PageInfo pageInfo = carService.selectByPage(pageNo, pageSize,carp,carid,Integer.parseInt(rent),status);
        CarVoThree carVoTwo = new CarVoThree();
        carVoTwo.setRent(rent);
        carVoTwo.setCarid(carid);
        carVoTwo.setCarp(carp);
        if (status ==null ||"".equals(status)){
            carVoTwo.setStatus(3);
        }else {
            carVoTwo.setStatus(status);
        }
        if (carid ==null ||"".equals(carid)){
            carVoTwo.setCarid(0);
        }else {
            carVoTwo.setCarid(carid);
        }
        // 将搜索条件的值添加到模型中
        model.addAttribute("car", carVoTwo);
        List<CarType> list=carTypeService.selectAll();
        model.addAttribute("list",list);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        return "/car/list";
    }
}
